package com.rock.code.bigdata.load.controller;

import com.github.pagehelper.PageInfo;
import com.rock.code.base.controller.BaseController;
import com.rock.code.bigdata.load.service.LoadService;
import com.rock.code.common.exceptions.SystemException;
import com.rock.code.common.pager.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by SJ on 2018/7/27.
 */

@Controller
@RequestMapping("/load")
public class LoadController extends BaseController {

    @Resource
    private LoadService loadServiceImpl;

    /**
     * 初始化加载页面
     *
     * @param UNIT_ID
     * @return
     */
    @RequestMapping("/{UNIT_ID}")
    public String host(@PathVariable("UNIT_ID") String UNIT_ID) {
        this.request.setAttribute("UNIT_ID", UNIT_ID);
        return "bigdata/load/loadpage";
    }

    /**
     * 仪表盘、横_柱状图
     *
     * @param EXT_UNIT_ID
     * @param KPI_CODE
     * @param DATE_TYPE
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryNum", method = RequestMethod.POST)
    public List queryNum(String EXT_UNIT_ID, String KPI_CODE, String DATE_TYPE, String CLASS_NAME) throws Exception {
        HashMap map = new HashMap();
        if (EXT_UNIT_ID != null && KPI_CODE != null && CLASS_NAME != null) {
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
            map.put("KPI_CODE", KPI_CODE);
            map.put("CLASS_NAME", CLASS_NAME);
        }
        String tjKpiCode = loadServiceImpl.tjKpi(map);
        if (tjKpiCode != null) {
            map.put("KPI_CODE", tjKpiCode);
        }
        if ("showWeek".equals(DATE_TYPE)) {
            String weekTableName = loadServiceImpl.weekTableName();
            String lastWeek = loadServiceImpl.lastWeek();
            if (weekTableName != null && lastWeek != null) {
//                map.put("tableName", weekTableName);
                map.put("tableName", "tb_kpi_value_week_2018");
//                map.put("DATA_TIME", lastWeek);
                map.put("DATA_TIME", "1");
            }
        } else if ("showMonth".equals(DATE_TYPE)) {
            String yearTableName = loadServiceImpl.yearTableName();
            String lastMonth = loadServiceImpl.lastMonth();
            if (yearTableName != null && lastMonth != null) {
//                map.put("tableName", yearTableName);
                map.put("tableName", "tb_kpi_value_year_2018");
//                map.put("DATA_TIME", lastMonth);
                map.put("DATA_TIME", "2018-01");
            }
        } else if ("showYear".equals(DATE_TYPE)) {
            String yearTableName = loadServiceImpl.yearTableName();
            if (yearTableName != null) {
                map.put("tableName", yearTableName);
            }
        } else {
            String tableName = loadServiceImpl.monthTableName();
            String yesterday = loadServiceImpl.getYesTerday();
            if (tableName != null && yesterday != null) {
//                map.put("tableName", tableName);
                map.put("tableName", "tb_kpi_value_month_201801");
//                map.put("DATA_TIME", yesterday);
                map.put("DATA_TIME", "2018-01-01");
            }
        }
        List list = loadServiceImpl.queryState(map);
        return list;
    }

    /**
     * 折线图
     *
     * @param EXT_UNIT_ID
     * @param KPI_CODE
     * @param DATE_TYPE
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryLoad", method = RequestMethod.POST)
    public HashMap queryLoad(String EXT_UNIT_ID, String KPI_CODE, String DATE_TYPE, String CLASS_NAME) throws Exception {
        HashMap map = new HashMap();
        HashMap mapTb = new HashMap();
        HashMap mapHb = new HashMap();
        HashMap resultMap = new HashMap();
        List list = new ArrayList();
        List listTb = new ArrayList();
        List listHb = new ArrayList();
        if (EXT_UNIT_ID != null && KPI_CODE != null && CLASS_NAME != null) {
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
            map.put("KPI_CODE", KPI_CODE);
            map.put("CLASS_NAME", CLASS_NAME);
            mapTb.put("EXT_UNIT_ID", EXT_UNIT_ID);
            mapTb.put("KPI_CODE", KPI_CODE);
            mapTb.put("CLASS_NAME", CLASS_NAME);
            mapHb.put("EXT_UNIT_ID", EXT_UNIT_ID);
            mapHb.put("KPI_CODE", KPI_CODE);
            mapHb.put("CLASS_NAME", CLASS_NAME);
        }

        String tjKpiCode = loadServiceImpl.tjKpi(map);
        if (tjKpiCode != null) {
            map.put("KPI_CODE", tjKpiCode);
            mapTb.put("KPI_CODE", tjKpiCode);
            mapHb.put("KPI_CODE", tjKpiCode);
        }
        if ("showWeek".equals(DATE_TYPE)) {
            resultMap = loadServiceImpl.queryLineByDate(EXT_UNIT_ID, (String) map.get("KPI_CODE"));
            return resultMap;
        } else if ("showMonth".equals(DATE_TYPE)) {
            String tableName = loadServiceImpl.lastMonthTableName();//201807
//            map.put("tableName", tableName);
//            map.put("tableName", "tb_kpi_value_month_201801");
            map.put("tableName", "tb_kpi_value_month_201801");//tb_kpi_value_month_201803
            String tableTbName = loadServiceImpl.lastYearlastMonth();//201707
//            mapTb.put("tableName", tableTbName);
//            mapTb.put("tableName", "tb_kpi_value_month_201710");
            mapTb.put("tableName", "tb_kpi_value_month_201801");//tb_kpi_value_month_201703
            String tableHbName = loadServiceImpl.lastTwoMonthTableName();//201806
//            mapHb.put("tableName", tableHbName);
//            mapHb.put("tableName", "tb_kpi_value_month_201809");
            mapHb.put("tableName", "tb_kpi_value_month_201801");//tb_kpi_value_month_201802
        } else if ("showYear".equals(DATE_TYPE)) {
            String tableName = loadServiceImpl.yearTableName();//2018
//            map.put("tableName", tableName);
            map.put("tableName", "tb_kpi_value_year_2018");
            String tableTbName = loadServiceImpl.lastYearTableName();//2017
//            mapTb.put("tableName", tableTbName);
            mapTb.put("tableName", "tb_kpi_value_year_2018");
        } else {
            String tableName = loadServiceImpl.queryTableName();
//            map.put("tableName", tableName);
//            map.put("tableName", "tb_kpi_value_day_20181129");
            map.put("tableName", "tb_kpi_value_day_20180101");
            String tableTbName = loadServiceImpl.queryTbTableName();
//            mapTb.put("tableName", tableTbName);//需要修改
//            mapTb.put("tableName", "tb_kpi_value_day_20171129");//需要修改
            mapTb.put("tableName", "tb_kpi_value_day_20180101");//需要修改
            String tableHbName = loadServiceImpl.getLastMonthYesTerday();
//            mapHb.put("tableName", tableHbName);//需要修改
//            mapHb.put("tableName", "tb_kpi_value_day_20180129");//需要修改
            mapHb.put("tableName", "tb_kpi_value_day_20180101");//需要修改
        }

        if ("showYear".equals(DATE_TYPE)) {
            list = loadServiceImpl.queryLoad(map);
            listTb = loadServiceImpl.queryLoad(mapTb);
            resultMap.put("list", list);
            resultMap.put("listTb", listTb);
        } else {
            list = loadServiceImpl.queryLoad(map);
            listTb = loadServiceImpl.queryLoad(mapTb);
            listHb = loadServiceImpl.queryLoad(mapHb);
        }
        resultMap.put("list", list);
        resultMap.put("listTb", listTb);
        resultMap.put("listHb", listHb);
        return resultMap;
    }


    /**
     * 表格
     *
     * @param EXT_UNIT_ID
     * @param RESU
     * @param DATE_TYPE
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTable", method = RequestMethod.POST)
    public HashMap queryTable(String EXT_UNIT_ID, String RESU, String DATE_TYPE, String CLASS_NAME) throws Exception {
        HashMap map = new HashMap();
        HashMap resultMap = new HashMap();
        List<String> KPI_CODE = Arrays.asList(RESU.split(","));
        for (int i = 0; i < KPI_CODE.size(); i++) {
            String KPI = KPI_CODE.get(i);
            if (EXT_UNIT_ID != null && KPI_CODE != null && CLASS_NAME != null) {
                map.put("EXT_UNIT_ID", EXT_UNIT_ID);
                map.put("KPI_CODE", KPI);
                map.put("CLASS_NAME", CLASS_NAME);
            }
            String tjKpiCode = loadServiceImpl.tjKpi(map);
            if (tjKpiCode != null) {
                map.put("KPI_CODE", tjKpiCode);
            }
            if ("showWeek".equals(DATE_TYPE)) {
                String weekTable = loadServiceImpl.weekTableName();
                String week = loadServiceImpl.lastWeek();
//                map.put("tableName", weekTable);
                map.put("tableName", "tb_kpi_value_week_2018");
//                map.put("DATA_TIME", week);
                map.put("DATA_TIME", "1");
            } else if ("showMonth".equals(DATE_TYPE)) {
                String monthTable = loadServiceImpl.yearTableName();
                String month = loadServiceImpl.lastMonth();
//                map.put("tableName", monthTable);
                map.put("tableName", "tb_kpi_value_year_2018");
//                map.put("DATA_TIME", month);
                map.put("DATA_TIME", "2018-10");
            } else if ("showYear".equals(DATE_TYPE)) {
                String yearTableName = loadServiceImpl.yearTableName();
                map.put("tableName", yearTableName);
            } else {
                String tableName = loadServiceImpl.monthTableName();
                String yesterday = loadServiceImpl.getYesTerday();
//                map.put("tableName", tableName);
                map.put("tableName", "tb_kpi_value_month_201801");
//                map.put("DATA_TIME", yesterday);
                map.put("DATA_TIME", "2018-10-01");
            }
            List list = loadServiceImpl.queryTable(map);
            resultMap.put(KPI, list);//用KPI做key
        }
        return resultMap;
    }


    /**
     * 基本信息
     *
     * @param EXT_UNIT_ID
     * @param RESU
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryInfo", method = RequestMethod.POST)
    public HashMap queryInfo(String EXT_UNIT_ID, String RESU, String CLASS_NAME) throws Exception {
        HashMap map = new HashMap();
        HashMap resultMap = new HashMap();
        try {
            List<String> KPI_CODE = Arrays.asList(RESU.split(","));
            for (int i = 0; i < KPI_CODE.size(); i++) {
                String KPI = KPI_CODE.get(i);
                if (EXT_UNIT_ID != null && KPI_CODE != null) {
                    map.put("EXT_UNIT_ID", EXT_UNIT_ID);
                    map.put("KPI_CODE", KPI);
                    map.put("CLASS_NAME", CLASS_NAME);
                }
                String tjKpiCode = loadServiceImpl.tjKpi(map);
                if (tjKpiCode != null) {
                    map.put("KPI_CODE", tjKpiCode);
                }
                List list = loadServiceImpl.queryInfo(map);
                resultMap.put(KPI, list);//用KPI做key
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultMap;
    }


    /**
     * 分页表格
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/queryTableByPage", method = RequestMethod.POST)
    public PageInfo<Map> queryTableByPage(String EXT_UNIT_ID, String DATE_TYPE) throws Exception {
        Map<String, Object> param = this.getRequestMapSingle();
        Page page = new Page(request);
        PageInfo pageInfo = null;
        try {
            if (EXT_UNIT_ID != null) {
                param.put("EXT_UNIT_ID", EXT_UNIT_ID);
            }
            if ("showWeek".equals(DATE_TYPE)) {
                String weekTable = loadServiceImpl.weekTableName();
                String week = loadServiceImpl.lastWeek();
//                param.put("tableName", weekTable);
                param.put("tableName", "tb_kpi_value_week_2018");
//                param.put("DATA_TIME", week);
                param.put("DATA_TIME", "1");
            } else if ("showMonth".equals(DATE_TYPE)) {
                String monthTable = loadServiceImpl.yearTableName();
                String month = loadServiceImpl.lastMonth();
//                param.put("tableName", monthTable);
                param.put("tableName", "tb_kpi_value_year_2018");
//                param.put("DATA_TIME", month);
                param.put("DATA_TIME", "2018-01");
            } else if ("showYear".equals(DATE_TYPE)) {
                String yearTableName = loadServiceImpl.yearTableName();
                param.put("tableName", yearTableName);
            } else {
                String tableName = loadServiceImpl.monthTableName();
                String yesterday = loadServiceImpl.getYesTerday();
//                param.put("tableName", tableName);
                param.put("tableName", "tb_kpi_value_month_201801");
//                param.put("DATA_TIME", yesterday);
                param.put("DATA_TIME", "2018-01-01");
            }
            if ("showYear".equals(DATE_TYPE)) {
                pageInfo = loadServiceImpl.queryPageListByYear(page, param);
            } else {
                pageInfo = loadServiceImpl.queryPageList(page, param);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return pageInfo;
    }


    /**
     * 柱状图
     *
     * @param EXT_UNIT_ID
     * @param KPI_CODE
     * @param DATE_TYPE
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryGraph", method = RequestMethod.POST)
    public HashMap queryGraph(String EXT_UNIT_ID, String KPI_CODE, String DATE_TYPE, String CLASS_NAME) throws Exception {
        HashMap map = new HashMap();
        HashMap resultMap = new HashMap();
        List list = new ArrayList();
        if (EXT_UNIT_ID != null && KPI_CODE != null) {
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
            map.put("KPI_CODE", KPI_CODE);
            map.put("CLASS_NAME", CLASS_NAME);
        }
        String tjKpiCode = loadServiceImpl.tjKpi(map);
        if (tjKpiCode != null) {
            map.put("KPI_CODE", tjKpiCode);
        }
        if ("showWeek".equals(DATE_TYPE)) {
            //先确定上周是这年的第几周，再去周表查出这周的天数，再去月表根据日期查出使用率
            Map map1 = new HashMap();
//            map1.put("lastweek", loadServiceImpl.lastWeek());
            map1.put("lastweek", "1");
//            map1.put("tableName", loadServiceImpl.weekTableName());
            map1.put("tableName", "tb_kpi_value_week_2018");
            map1.put("EXT_UNIT_ID", EXT_UNIT_ID);
            map1.put("KPI_CODE", KPI_CODE);
            List list1 = loadServiceImpl.queryDateByWeek(map1);//根据上周第几周查询该周从几号到几号
            //==============================注释掉部分参数写死不需要截========================
//            String str = "";
//            for (Object o : list1) {
//                System.out.println(o.toString());
//                str = o.toString();
//            }
//            String a = str.split(",")[0];
//            String b = str.split(",")[1];
//            String list_start = a.split("=")[1];//开始时间
//            System.out.println(list_start);
//            String d = b.split("=")[1];
//            String list_end = d.replace("}", "");//结束时间
//            System.out.println(list_end);
//            /****表名***/
//            String startTime = list_start.split("-")[0] + list_start.split("-")[1];
//            String tableName_one = "tb_kpi_value_month_" + startTime;
//            String endTime = list_end.split("-")[0] + list_end.split("-")[1];
//            String tableName_two = "tb_kpi_value_month_" + endTime;
            //====================================================================================
//            map.put("table_one", tableName_one);
            map.put("table_one", "tb_kpi_value_month_201801");
//            map.put("table_two", tableName_two);
            map.put("table_two", "tb_kpi_value_month_201801");
//            map.put("start_time", list_end);
            map.put("start_time", "2018-10-01");
//            map.put("end_time", list_start);
            map.put("end_time", "2018-10-07");
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
            map.put("KPI_CODE", KPI_CODE);
            List lastweek = loadServiceImpl.queryZztWeek(map);
            resultMap.put("list", lastweek);
            return resultMap;
        } else if ("showMonth".equals(DATE_TYPE)) {
            String tableName = loadServiceImpl.lastMonthTableName();//201807
//            map.put("tableName", tableName);
            map.put("tableName", "tb_kpi_value_month_201801");
        } else if ("showYear".equals(DATE_TYPE)) {
            String tableName = loadServiceImpl.yearTableName();//2018
            map.put("tableName", tableName);
        } else {
            String tableName = loadServiceImpl.queryTableName();
//            map.put("tableName", tableName);
            map.put("tableName", "tb_kpi_value_day_20180101");
        }
        list = loadServiceImpl.queryLoad(map);
        resultMap.put("list", list);
        return resultMap;
    }


    /**
     * 文件系统   表空间
     *
     * @param EXT_UNIT_ID
     * @param KPI_CODE
     * @param DATE_TYPE
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTableSpace", method = RequestMethod.POST)
    public HashMap queryTableSpace(String EXT_UNIT_ID, String KPI_CODE, String DATE_TYPE, String CLASS_NAME) throws Exception {
            HashMap map = new HashMap();
        Map map1 = new HashMap();
        HashMap resultMap = new HashMap();
        List list = new ArrayList();
        if (EXT_UNIT_ID != null && KPI_CODE != null) {
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
            map.put("KPI_CODE", KPI_CODE);
            map.put("CLASS_NAME", CLASS_NAME);
        }
        String tjKpiCode = loadServiceImpl.tjKpi(map);
        if (tjKpiCode != null) {
            map.put("KPI_CODE", tjKpiCode);
            map1.put("KPI_CODE", tjKpiCode);
        }
        if ("showWeek".equals(DATE_TYPE)) {
            //先确定上周是这年的第几周，再去周表查出这周的天数，再去月表根据日期查出使用率
//            map1.put("lastweek", loadServiceImpl.lastWeek());
            map1.put("lastweek", "1");
//            map1.put("tableName", loadServiceImpl.weekTableName());
            map1.put("tableName", "tb_kpi_value_week_2018");
            map1.put("EXT_UNIT_ID", EXT_UNIT_ID);
            List list1 = loadServiceImpl.queryDateByWeek(map1);//根据上周第几周查询该周从几号到几号
            //==============================注释掉部分参数写死不需要截========================
//            String str = "";
//            for (Object o : list1) {
//                System.out.println(o.toString());
//                str = o.toString();
//            }
//            String a = str.split(",")[0];
//            String b = str.split(",")[1];
//            String list_start = a.split("=")[1];//开始时间
//            System.out.println(list_start);
//            String d = b.split("=")[1];
//            String list_end = d.replace("}", "");//结束时间
//            System.out.println(list_end);
//            /****表名***/
//            String startTime = list_start.split("-")[0] + list_start.split("-")[1];
//            String tableName_one = "tb_kpi_value_month_" + startTime;
//            String endTime = list_end.split("-")[0] + list_end.split("-")[1];
//            String tableName_two = "tb_kpi_value_month_" + endTime;
            //======================================================
//            map.put("table_one", tableName_one);
            map.put("table_one", "tb_kpi_value_month_201801");
//            map.put("table_two", tableName_two);
            map.put("table_two", "tb_kpi_value_month_201801");
//            map.put("start_time", list_end);
            map.put("start_time", "2018-01-24");
//            map.put("end_time", list_start);
            map.put("end_time", "2018-01-31");
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
            List lastweek = loadServiceImpl.queryLineByDate1(map);
            resultMap.put("list", lastweek);
            return resultMap;
        } else if ("showMonth".equals(DATE_TYPE)) {
            String tableName = loadServiceImpl.lastMonthTableName();//201807
//            map.put("tableName", tableName);
            map.put("tableName", "tb_kpi_value_month_201801");
        } else if ("showYear".equals(DATE_TYPE)) {
            String tableName = loadServiceImpl.yearTableName();//2018
            map.put("tableName", tableName);
        } else {
            String tableName = loadServiceImpl.queryTableName();
//            map.put("tableName", tableName);
            map.put("tableName", "tb_kpi_value_day_20180101");
        }
        list = loadServiceImpl.queryTableSpace(map);
        resultMap.put("list", list);
        return resultMap;
    }
}